package com.igeek.shop.web.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.igeek.shop.entity.Order;
import com.igeek.shop.service.pro.ProductServiceImpl;


public class AdminOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProductServiceImpl ps = new ProductServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String state = request.getParameter("state");
		if(state.equals("queryOrder")) {
			this.queryOrder(request,response);
		}else if(state.equals("getOrderInfoByOid")) {
			this.getOrderInfoByOid(request,response);
		}
		
	}


	private void getOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String oid = request.getParameter("oid");
		Order order = new Order();
		order.setOid(oid);
		List<Map<String, Object>> orderItemList = ps.getOrderItemList(order);
		Gson gson = new Gson();
		String json = gson.toJson(orderItemList);
		//System.out.println(json);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(json);
	}


	private void queryOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Order> orderList = ps.getAllOrders();
		request.setAttribute("orderList", orderList);
		request.getRequestDispatcher("/admin/order/list.jsp").forward(request, response);
	}

}
